    <?php 
    function singleGenealogy($parent_level, $level = 0)  {
    	?>
    	
    	<?php 
    	if ($parent_level != null){
	       $children = Doctrine_Core::getTable('Level')->getLevelChildren($parent_level->getId());
	       if(count($children) > 0) {
	        	$level++;
		       	foreach ($children as $child) {
		       		$children_count = Doctrine_Core::getTable('Level')->countLevelChildren($parent_level->getId());
		       		$class = $children_count > 0 ? "jstree-open" : "jstree-leaf jstree-last";
		       		?>
		       		<li id="<?php echo $child->getId();?>" class = "<?php echo $class; ?>" >
		       		<a href="#">
		       		<?php 
		             echo $child->getName();
		             
		             ?>
		             </a>
		             <ul>
		             <?php 
		             singleGenealogy($child, $level);
		             ?>
		             </ul>
		             </li>
		             <?php 
		       	}
	       }
    	}
       ?>
       <?php 
    }
    
    ?>
    
    <div class="head"><h5 class="iList">Category Levels Tree</h5></div>
    <?php if ($root_level != null) {?>
    <div  id="demo3" class="demo">
    	<ul>
    		<li id="<?php echo $root_level->getId();?>" class="jstree-open">
				<a href="#"><?php echo $root_level->getName(); ?></a>
				<ul>
		    	<?php 
		    		singleGenealogy($root_level);
		    	?>
		    	</ul>
	    	</li>
		</ul>
    </div>
    <?php }?>

    
    <script>

    	$("#demo3")
    		.jstree({ "plugins" : ["themes","html_data","ui"] 
    	 })
    		
    		// 1) if using the UI plugin bind to select_node
    		.bind("select_node.jstree", function (event, data) { 
    			// `data.rslt.obj` is the jquery extended node that was clicked
    			var php_url = '<?php echo url_for('category_levels/show?id=')?>';
    			php_url = php_url.concat(data.rslt.obj.attr("id"));
    			$.ajax({type: 'POST', url: php_url, script:true, cache:false, success: function(data) {  $('#category_level_div').html(data);} });
    		})
    		// 2) if not using the UI plugin - the Anchor tags work as expected
    		//    so if the anchor has a HREF attirbute - the page will be changed
    		//    you can actually prevent the default, etc (normal jquery usage)
    		.delegate("a", "click", function (event, data) { event.preventDefault(); });

    
    </script>